#include "stdafx.h"

#include "MyControls/AddPointTool.h"	  
#include "MyControls/BezierEditor.h"

namespace My {

AddPointTool::AddPointTool(BezierEditor & owner)
	: m_Owner(owner)
{
	m_Hit = false;
}

void AddPointTool::Activate()
{
}

void AddPointTool::Deactivate()
{
	m_Hit = false;
}

void AddPointTool::PaintRect(CDC * pDC, const CRect & r)
{
}

void AddPointTool::OnLButtonDown(UINT nFlags, CPoint p)
{
	Size node;
	m_Hit = !m_Owner.NodeAt(p, m_Curve, node)
		&& m_Owner.CurveAt(p, m_Curve, m_Param);
}

void AddPointTool::OnLButtonDblClk(UINT nFlags, CPoint p)
{
}

void AddPointTool::OnLButtonUp(UINT nFlags, CPoint p)
{
	if (m_Hit) {
		Bezier & b = m_Owner.GetBezierOwner()->GetBezier(m_Curve);
		if ((GetAsyncKeyState(VK_SHIFT) & (~0 << 1)) == 0)
			m_Owner.GetBezierOwner()->DeselectAll();
		b.Select(b.Insert(m_Param));
		m_Hit = false;
	}
}

void AddPointTool::OnRButtonDown(UINT nFlags, CPoint p)
{
}

void AddPointTool::OnRButtonDblClk(UINT nFlags, CPoint p)
{
}

void AddPointTool::OnRButtonUp(UINT nFlags, CPoint p)
{
}

void AddPointTool::OnMouseMove(UINT nFlags, CPoint p)
{
	if (m_Hit) {
		Bezier & b = m_Owner.GetBezierOwner()->GetBezier(m_Curve);
		if ((GetAsyncKeyState(VK_SHIFT) & (~0 << 1)) == 0)
			m_Owner.GetBezierOwner()->DeselectAll();
		Size node = b.Insert(m_Param);
		b.Select(node);
		BezierNode n = b.GetNode(node);
		n.type = bnSymmetrical;
		n.control[0] = n.control[1] = n.position;
		m_Owner.GetBench().SetManipulator(
			std::auto_ptr<Manipulator>(new DragControlPoint(
			m_Owner,
			b,
			node, 
			1,
			p,
			&n)));
		m_Hit = false;
	}
}

void AddPointTool::OnMouseEnter(UINT nFlags, CPoint p)
{
}

void AddPointTool::OnMouseLeave()
{
}

void AddPointTool::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
}

void AddPointTool::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
}

bool AddPointTool::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	SetCursor(GetApp().LoadCursor("CURSOR_PEN_ADD"));
	return true;
}

} //namespace My
